package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcElementAttachRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.ecelement.EcElementAttach;
import com.ndp.fb.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by evan on 2016/7/7.
 */
@Service
public class EcElementAttachRdbServiceImpl extends DaoTemplate implements EcElementAttachRdbService {

    @Override
    public EcElementAttach save(EcElementAttach ecElementAttach) {
        Long id = saveResultId(ecElementAttach);
        return findOneByProperty("id", id, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public void update(EcElementAttach ecElementAttach) {
        String sql  = "update ec_element_attach set is_delete = 0,attach_element_img = ? where 1=1 and id = ? ";
        getJdbcTemplate().update(sql, new Object[]{ecElementAttach.getAttachElementImg(), ecElementAttach.getId()});
    }

    @Override
    public List<EcElementAttach> findByType(Long orgId, Integer type) {
        String sql = "select * from ec_element_attach where is_delete = 0 and org_id = ? and attach_element_type = ?";
        return getJdbcTemplate().query(sql, new Object[]{orgId,type}, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public List<EcElementAttach> findByOrgId(Long orgId) {
        String sql = "select * from ec_element_attach where is_delete = 0 and org_id = ? order by  attach_element_type, create_time desc ";
        return getJdbcTemplate().query(sql, new Object[]{orgId}, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public EcElementAttach findByCodeAndType(String code1, String code2, Integer type, Long orgId) {
        List paramValue = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select * from ec_element_attach where 1=1 and attach_element_code1 = ? and attach_element_type = ? and org_id = ?");
        paramValue.add(code1);
        paramValue.add(type);
        paramValue.add(orgId);
        if(StringUtil.isNotEmpty(code2)) {
            sql.append(" and attach_element_code2 =?");
            paramValue.add(code2);
        }

        return findOne(sql.toString(), paramValue.toArray(), DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public List<EcElementAttach> findByCode(String code, Long orgId) {
        String sql = "select * from ec_element_attach where 1=1 and attach_element_code = ?  and org_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{code, orgId}, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public EcElementAttach findBg(Long templateId, Long orgId) {
        String sql = "SELECT a.* FROM ec_element_attach a, ec_template b WHERE a.org_id = ? AND b.id= ? AND CONCAT(b.template_bg_type, b.template_bg_style) = CONCAT(a.attach_element_code1, a.attach_element_code2)";
        return findOne(sql, new Object[]{orgId, templateId}, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public EcElementAttach findBk(Long templateId, Long orgId) {
        String sql = "SELECT a.* FROM ec_element_attach a, ec_template b WHERE a.org_id = ? AND b.id= ? AND b.template_bk = a.attach_element_code1";
        return findOne(sql, new Object[]{orgId, templateId}, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }

    @Override
    public Pager<EcElementAttach> getAllDesignEle(Long orgId, Integer type, Integer firstResult, Integer sizeNo) {
        List paramValue = new ArrayList<>();
        paramValue.add(orgId);
        StringBuffer sql = new StringBuffer("select * from ec_element_attach where 1=1 and org_id = ? ");
        if(type !=null && type != 0) {
            sql.append(" and attach_element_type = ?");
            paramValue.add(type);
        }
        sql.append(" order by  attach_element_type, create_time desc");
        return findPager(sql.toString(), paramValue.toArray(), firstResult, sizeNo, DefaultRowMapper.autoRowMapper(EcElementAttach.class));
    }
}
